package com.xcjw.com.Activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xcjw.com.Base.Myapplication;
import com.xcjw.com.R;
import com.xcjw.com.Utils.StatusbarUtils;
import com.xcjw.com.View.ObservableScrollView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @packname com.xcjw.com.Activity
 * @filename MouthActivity
 * @date on 2018/11/21 10:24
 *****/
public class MouthActivity extends AppCompatActivity implements View.OnClickListener, ObservableScrollView.OnObservableScrollViewListener {

      @BindView(R.id.tv_main_topContent)
      TextView tvMainTopContent;

      @BindView(R.id.sv_main_content)
      ObservableScrollView svMainContent;
      @BindView(R.id.iv_header_left)
      ImageView ivHeaderLeft;
      @BindView(R.id.tv_header_title)
      TextView tvHeaderTitle;
      @BindView(R.id.iv_header_img)
      ImageView ivHeaderImg;
      @BindView(R.id.relayout)
      RelativeLayout relayout;
      @BindView(R.id.ll_header_content)
      LinearLayout llHeaderContent;
      private ObservableScrollView mObservableScrollView;
      private TextView mImageView;
      private LinearLayout mHeaderContent;
      private int mHeight;
      private int month;

      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            StatusbarUtils.enableTranslucentStatusbar(this);
            setContentView(R.layout.mouth_activity);
            ButterKnife.bind(this);
            Calendar aCalendar = Calendar.getInstance();
            month = aCalendar.get(Calendar.MONTH)+1;
            List<String> dayNextOfMonth = getDayNextOfMonth(month);
            int result = 0;
            int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                  result = this.getResources().getDimensionPixelSize(resourceId);
            }

            Myapplication.getInstance().addActivity(this);
            llHeaderContent.setPadding(0,result,0,0);
            //初始化控件
            mObservableScrollView = (ObservableScrollView) findViewById(R.id.sv_main_content);

            mHeaderContent = (LinearLayout) findViewById(R.id.ll_header_content);


            //获取标题栏高度
            ViewTreeObserver viewTreeObserver = mImageView.getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                  @Override
                  public void onGlobalLayout() {

                        //注册滑动监听
                        mObservableScrollView.setOnObservableScrollViewListener(MouthActivity.this);
                  }
            });


      }
      /**
       * 获取下个月的日期集合
       * @param months
       * @return
       */
      public static List<String> getDayNextOfMonth(Integer months){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            List<String> list = new ArrayList<>();
            Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
            aCalendar.set(aCalendar.MONTH, months);
            int year = aCalendar.get(Calendar.YEAR);//年份
            int month = aCalendar.get(Calendar.MONTH)+1;//月份
            int day = aCalendar.getActualMaximum(Calendar.DATE);


            for (int i = 1; i <= day; i++) {

                  if (i<=9) {
                        String aDate = String.valueOf(year)+"-"+month+"-0"+i;
                        list.add(aDate);
                  }else {
                        String aDate = String.valueOf(year)+"-"+month+"-"+i;
                        list.add(aDate);
                  }

            }
            return list;
      }
      //获取当前月

    ;//月份
      @Override
      public void onClick(View v) {

      }

      @Override
      public void onObservableScrollViewListener(int l, int t, int oldl, int oldt) {
            if (t <= 0) {
                  //顶部图处于最顶部，标题栏透明
                  mHeaderContent.setBackgroundColor(Color.argb(0, 48, 63, 159));
            } else if (t > 0 && t < mHeight) {
                  //滑动过程中，渐变
                  float scale = (float) t / mHeight;//算出滑动距离比例
                  float alpha = (255 * scale);//得到透明度
                  mHeaderContent.setBackgroundColor(Color.argb((int) alpha, 48, 63, 159));
            } else {
                  //过顶部图区域，标题栏定色
                  mHeaderContent.setBackgroundColor(Color.argb(255, 48, 63, 159));
            }
      }


}